package com.yotta.operationdatascreencode.common.core;

import com.yotta.operationdatascreencode.constants.CommonConstants;
import io.swagger.v3.oas.annotations.media.Schema;
import java.io.Serializable;
import lombok.Data;

/**
 * 公共返回
 * @param <T> 泛型
 */
@Data
@Schema(description = "统一返回")
public class R<T> implements Serializable {
    private static final long serialVersionUID = 1L;


    public void setCode(int code) {
        this.code = code;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public void setData(T data) {
        this.data = data;
    }

    @Schema(description = "状态码 200成功500失败400业务异常403令牌异常")
    private int code;
    @Schema(description = "消息")
    private String msg;
    @Schema(description = "数据")
    private T data;

    public int getCode() {
        return code;
    }

    public String getMsg() {
        return msg;
    }

    public T getData() {
        return data;
    }

    public static <T> R<T> ok() {
        return restResult(null, CommonConstants.SUCCESS, null);
    }

    public static <T> R<T> ok(T data) {
        return restResult(data, CommonConstants.SUCCESS, null);
    }

    public static <T> R<T> ok(T data, String msg) {
        return restResult(data, CommonConstants.SUCCESS, msg);
    }

    public static <T> R<T> failed() {
        return restResult(null, CommonConstants.FAIL, null);
    }

    public static <T> R<T> failed(String msg) {
        return restResult(null, CommonConstants.FAIL, msg);
    }

    public static <T> R<T> failed(T data) {
        return restResult(data, CommonConstants.FAIL, null);
    }

    public static <T> R<T> failed(T data, String msg) {
        return restResult(data, CommonConstants.FAIL, msg);
    }

    /**
     * 构造方法
     * @param data 数据
     * @param code 状态码
     * @param msg 消息
     * @param <T> 泛型
     * @return 返回值
     */
    public static <T> R<T> restResult(T data, int code, String msg) {
        R<T> apiResult = new R<>();
        apiResult.setCode(code);
        apiResult.setData(data);
        apiResult.setMsg(msg);
        return apiResult;
    }

    public static <T> R<T> error() {
        return restResult(null, CommonConstants.ERROR, null);
    }

    public static <T> R<T> error(T data) {
        return restResult(data, CommonConstants.ERROR, null);
    }

    public static <T> R<T> error(T data, String msg) {
        return restResult(data, CommonConstants.ERROR, msg);
    }

    public static <T> R<T> errorAuth() {
        return restResult(null, CommonConstants.TOKEN_OVERDUE, null);
    }

    public static <T> R<T> errorAuth(T data) {
        return restResult(data, CommonConstants.TOKEN_OVERDUE, null);
    }

    public static <T> R<T> errorAuth(T data, String msg) {
        return restResult(data, CommonConstants.TOKEN_OVERDUE, msg);
    }

}
